64. SharePoint Module

Note

The below information is extensively based in information taken from the PowerShell® Notes for Professionals book. I plan to extend this information based on my day to day usage of the language.

64.1: Loading SharePoint Snap-In

Loading the SharePoint Snapin can be done using the following:

Add-PSSnapin "Microsoft.SharePoint.PowerShell"

This only works in the 64bit version of PowerShell. If the window says "Windows PowerShell (x86)" in the title you are using the incorrect version.

If the Snap-In is already loaded, the code above will cause an error. Using the following will load only if necessary, which can be used in Cmdlets/functions:

1
2
3
4
if (( Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
  Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

Alternatively, if you start the SharePoint Management Shell, it will automatically include the Snap-In. To get a list of all the available SharePoint Cmdlets, run the following:

Get-Command - Module Microsoft.SharePoint.PowerShell

64.2: Iterating over all lists of a site collection

Print out all list names and the item count.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$site = Get-SPSite -Identity https://mysharepointsite/sites/test
foreach ($web in $site.AllWebs)
{
  foreach ($list in $web.Lists)
  {
    # Prints list title and item count
    Write-Output "$($list.Title), Items: $($list.ItemCount)"
  }
}
$site.Dispose()

64.3: Get all installed features on a site collection

Get-SPFeature -Site https://mysharepointsite/sites/test

Get-SPFeature can also be run on web scope (-Web ), farm scope (-Farm) and web application scope (-WebApplication ).

Get all orphaned features on a site collection

Another usage of Get-SPFeature can be to find all features that have no scope:

1
Get-SPFeature -Site https://mysharepointsite/sites/test |? { $_.Scope -eq $null ) }